$(function(){
    var mobile = getQueryString("mobile");
    var captcha = getQueryString("captcha");
    
    // 显示密码
    $('#checkbox').click(function(){
        if ($(this).prop('checked')) {
            $('#password').attr('type', 'text');
        } else {
            $('#password').attr('type', 'password');
        }
    });

    $.sValid.init({//注册验证
        rules:{
            password:"required",
            area_info:"required",
        },
        messages:{
            password:"密码必填!",
            area_info:"地区必填！",
        },
        callback:function (eId,eMsg,eRules){
            if(eId.length >0){
                var errorHtml = "";
                $.map(eMsg,function (idx,item){
                    errorHtml += "<p>"+idx+"</p>";
                });
                errorTipsShow(errorHtml);
            }else{
                errorTipsHide()
            }
        }  
    });
    
    $('#completebtn').click(function(){
        if (!$(this).parent().hasClass('ok')) {
            return false;
        }
        var password = $("#password").val();
        var province_id = $('#area_info').attr('data-areaid1');
        var city_id = $('#area_info').attr('data-areaid2');
        var area_id = $('#area_info').attr('data-areaid');
        var area_info = $('#area_info').val();
        if($.sValid()){
            $.ajax({
                type:'post',
                url:ApiUrl+"/index.php?act=connect&op=sms_register",  
                data:{
                    phone:mobile,
                    captcha:captcha,
                    password:password,
                    client:'wap',
                    province_id:province_id,
                    city_id:city_id,
                    area_id:area_id,
                    area_info:area_info,
                    type:2
                },
                dataType:'json',
                success:function(result){
                    if(!result.datas.error){
                        addCookie('username',result.datas.username);
                        addCookie('key',result.datas.key);
                        location.href = DBSITE+result.datas.token;
                    }else{
                        errorTipsShow("<p>"+result.datas.error+"</p>");
                    }
                }
            });         
        }
    });

    // 选择地区
    $('#area_info').on('click', function(){
        $.areaSelected({
            success : function(data){
                console.log(data);
                $('#area_info').val(data.area_info).attr({'data-areaid':data.area_id,'data-areaid1':data.area_id_1 , 'data-areaid2':(data.area_id_2 == 0 ? data.area_id_1 : data.area_id_2),'data-areaid3':(data.area_id_3 == 0 ? data.area_id_2 : data.area_id_3)});
            }
        });
    });
});


